package com.fx.zmlzml.controller;

import com.fx.zmlzml.common.response.Result;
import com.fx.zmlzml.entity.PcGpu;
import com.fx.zmlzml.exception.ErrorEnum;
import com.fx.zmlzml.service.PcGpuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电脑显卡控制器
 */
@RestController
@RequestMapping("/api/gpu")
@Tag(name = "电脑显卡管理", description = "提供电脑显卡的CRUD操作及相关查询功能")
@Slf4j
public class PcGpuController {

    @Autowired
    private PcGpuService pcGpuService;

    /**
     * 分页查询显卡列表
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询显卡列表", description = "分页查询系统中的电脑显卡信息")
    public Result<PageInfo<PcGpu>> pageGpus(
            @Parameter(description = "页码，默认为1", example = "1", required = false) @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数，默认为15", example = "15", required = false) @RequestParam(defaultValue = "15") Integer pageSize) {
        try {
            log.info("分页查询显卡列表请求，页码: {}, 每页条数: {}", pageNum, pageSize);

            // 开启分页查询
            PageHelper.startPage(pageNum, pageSize);
            List<PcGpu> list = pcGpuService.list();
            PageInfo<PcGpu> pageInfo = new PageInfo<>(list);

            log.info("分页查询显卡列表成功，共 {} 条记录", pageInfo.getTotal());
            return Result.i18nSuccess(pageInfo, "gpu.query.success", null);
        } catch (Exception e) {
            log.error("分页查询显卡列表失败", e);
            return Result.i18nError(ErrorEnum.GPU_QUERY_FAILED.getCode(), "gpu.query.fail", null);
        }
    }

    /**
     * 根据ID获取显卡详情
     * @param id 显卡ID
     * @return 显卡详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取显卡详情", description = "根据唯一标识符查询电脑显卡的详细信息")
    public Result<PcGpu> getGpuById(
            @Parameter(description = "显卡ID", example = "1", required = true) @PathVariable Long id) {
        try {
            log.info("根据ID获取显卡详情请求，ID: {}", id);
            PcGpu gpu = pcGpuService.getById(id);
            if (gpu == null) {
                log.warn("根据ID获取显卡详情失败，显卡不存在，ID: {}", id);
                return Result.i18nError(ErrorEnum.GPU_NOT_FOUND.getCode(), "gpu.not.found", null);
            }
            log.info("根据ID获取显卡详情成功，ID: {}", id);
            return Result.i18nSuccess(gpu, "gpu.query.success", null);
        } catch (Exception e) {
            log.error("根据ID获取显卡详情失败，ID: {}", id, e);
            return Result.i18nError(ErrorEnum.GPU_QUERY_FAILED.getCode(), "gpu.query.fail", null);
        }
    }

    /**
     * 添加显卡
     * @param gpu 显卡信息
     * @return 添加结果
     */
    @PostMapping
    @Operation(summary = "添加显卡", description = "向系统中添加新的电脑显卡信息")
    public Result<Boolean> addGpu(
            @Parameter(description = "显卡信息", required = true) @Validated @RequestBody PcGpu gpu) {
        try {
            log.info("添加显卡请求: {}", gpu);
            boolean success = pcGpuService.save(gpu);
            if (success) {
                log.info("添加显卡成功");
                return Result.i18nSuccess(true, "gpu.add.success", null);
            } else {
                log.error("添加显卡失败");
                return Result.i18nError(ErrorEnum.GPU_ADD_FAILED.getCode(), "gpu.add.fail", null);
            }
        } catch (Exception e) {
            log.error("添加显卡失败", e);
            return Result.i18nError(ErrorEnum.GPU_ADD_FAILED.getCode(), "gpu.add.fail", null);
        }
    }

    /**
     * 更新显卡信息
     * @param id 显卡ID
     * @param gpu 显卡信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新显卡信息", description = "更新系统中指定的电脑显卡信息")
    public Result<Boolean> updateGpu(
            @Parameter(description = "显卡ID", example = "1", required = true) @PathVariable Integer id,
            @Parameter(description = "显卡信息", required = true) @Validated @RequestBody PcGpu gpu) {
        try {
            log.info("更新显卡信息请求，ID: {}, 信息: {}", id, gpu);
            gpu.setId(id);
            boolean success = pcGpuService.updateById(gpu);
            if (success) {
                log.info("更新显卡信息成功，ID: {}", id);
                return Result.i18nSuccess(true, "gpu.update.success", null);
            } else {
                log.error("更新显卡信息失败，ID: {}", id);
                return Result.i18nError(ErrorEnum.GPU_UPDATE_FAILED.getCode(), "gpu.update.fail", null);
            }
        } catch (Exception e) {
            log.error("更新显卡信息失败，ID: {}", id, e);
            return Result.i18nError(ErrorEnum.GPU_UPDATE_FAILED.getCode(), "gpu.update.fail", null);
        }
    }

    /**
     * 删除显卡
     * @param id 显卡ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除显卡", description = "从系统中删除指定的电脑显卡信息")
    public Result<Boolean> deleteGpu(
            @Parameter(description = "显卡ID", example = "1", required = true) @PathVariable Long id) {
        try {
            log.info("删除显卡请求，ID: {}", id);
            boolean success = pcGpuService.removeById(id);
            if (success) {
                log.info("删除显卡成功，ID: {}", id);
                return Result.i18nSuccess(true, "gpu.delete.success", null);
            } else {
                log.error("删除显卡失败，ID: {}", id);
                return Result.i18nError(ErrorEnum.GPU_DELETE_FAILED.getCode(), "gpu.delete.fail", null);
            }
        } catch (Exception e) {
            log.error("删除显卡失败，ID: {}", id, e);
            return Result.i18nError(ErrorEnum.GPU_DELETE_FAILED.getCode(), "gpu.delete.fail", null);
        }
    }
}